package main

import (
	"fmt"
	"image/color"

	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"github.com/skip2/go-qrcode"
)

func main_test() {
	var mainWindow *walk.MainWindow
	var qrImage *walk.ImageView

	// 创建主窗口
	_, err := MainWindow{
		AssignTo: &mainWindow,
		Title:    "二维码生成器",
		MinSize:  Size{300, 400},
		Layout:   VBox{},
		Children: []Widget{
			HSplitter{
				Children: []Widget{
					PushButton{
						MaxSize: Size{100, 40},
						Text:    "生成二维码",
						OnClicked: func() {
							text := "hello"
							if text == "" {
								return
							}

							// 生成二维码
							qr, err := qrcode.New(text, qrcode.Medium)
							if err != nil {
								walk.MsgBox(mainWindow, "错误",
									fmt.Sprintf("生成二维码失败: %v", err),
									walk.MsgBoxIconError)
								return
							}
							qr.ForegroundColor = color.Black
							qr.BackgroundColor = color.White

							// 将二维码转换为图像
							img := qr.Image(256)

							// 显示二维码
							bitmap, err := walk.NewBitmapFromImage(img)
							if err != nil {
								walk.MsgBox(mainWindow, "错误",
									fmt.Sprintf("创建位图失败: %v", err),
									walk.MsgBoxIconError)
								return
							}
							qrImage.SetImage(bitmap)
						},
					},
				},
			},
			ImageView{
				AssignTo: &qrImage,
				MinSize:  Size{256, 256},
			},
		},
	}.Run()

	if err != nil {
		fmt.Printf("运行错误: %v\n", err)
	}
}
